package com.lgj.springboot_lgj.web.controller;


import com.github.pagehelper.PageInfo;
import com.lgj.springboot_lgj.bean.basic.Carousel;
import com.lgj.springboot_lgj.service.ICarouselService;
import com.lgj.springboot_lgj.utils.EntityUtil;
import com.lgj.springboot_lgj.utils.Result;
import com.lgj.springboot_lgj.utils.ResultUtil;
import com.lgj.springboot_lgj.web.vm.CarouselVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "轮播图模块")
@RestController
@RequestMapping("/auth/carousel")
public class CarouseController {
    @Autowired
    private ICarouselService service;
    @ApiOperation("删除轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "编号",required = true),
    })
    @DeleteMapping("/deleteById")
    public Result deleteById(Integer id){
     service.deleteById(id);
     return ResultUtil.success("删除成功");
    }
    @ApiOperation("查询所有已经启用的轮播图")
    @GetMapping("/find/yes")
    public  Result findByYes(){
        List<Carousel> list = service.findYesCarousel();
        return ResultUtil.success(list);
    }
    @ApiOperation("分页查询所有轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页大小",required = true)
    })
    @GetMapping("/page/query")
    public  Result findAllByPage(Integer pageNum,Integer pageSize){
        PageInfo<Carousel> pageInfo = service.findAll(pageNum, pageSize);
        return ResultUtil.success(pageInfo);
    }
    @ApiOperation("保存轮播图信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carouselInfo",value = "轮播图描述信息",required = true),
            @ApiImplicitParam(name = "carouselName",value = "轮播图名称",required = true),
            @ApiImplicitParam(name = "carouselUrl",value = "轮播图地址",required = true)
    })
    @PostMapping("/save")
    public  Result save(CarouselVM vm){
        service.saveCarousel(EntityUtil.vm2Entity(vm,Carousel.class));
        return ResultUtil.success("新增成功");
    }
    @ApiOperation("更新轮播图信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carouselId",value = "轮播图id",required = true),
            @ApiImplicitParam(name = "carouselInfo",value = "轮播图描述信息",required = false),
            @ApiImplicitParam(name = "carouselName",value = "轮播图名称",required = false),
            @ApiImplicitParam(name = "carouselStatus",value = "轮播图状态",required = false),
            @ApiImplicitParam(name = "carouselUrl",value = "轮播图地址",required = false)
    })
    @PutMapping("/update")
    public Result update(Carousel vm){
        service.updateCarousel(vm);
        return ResultUtil.success("更新成功");
    }
    @ApiOperation("查询轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "轮播图名称",required = true)
    })
    @GetMapping("/findByName")
    public Result findOneByName(String name){
        List<Carousel> oneByName = service.findOneByName(name);
        return ResultUtil.success(oneByName);
    }
}
